package com.ruoyi.web.controller.SignWish;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.Dictionary.domain.Zd;
import com.ruoyi.Dictionary.service.IZdService;
import com.ruoyi.HbUser.domain.HbUser;
import com.ruoyi.HbUser.service.IHbUserService;
import com.ruoyi.SignWish.domain.SignWish;
import com.ruoyi.SignWish.service.ISignWishService;
import com.ruoyi.User.domain.User;
import com.ruoyi.User.service.IUserService;
import com.ruoyi.UserAddress.domain.UserAddress;
import com.ruoyi.UserAddress.service.IUserAddressService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateTools;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 心愿单 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-09-02
 */
@Controller
@RequestMapping("/SignWish/signWish")
public class SignWishController extends BaseController
{
    private String prefix = "SignWish/signWish";
    
    @Autowired
	private IUserService userService;
	
	@Autowired
	private ISignWishService signWishService;
	
	@Autowired
	private IUserAddressService userAddressService;
	
	@Autowired
	private IHbUserService hbUserService;
	
	@Autowired
	private IZdService zdService;
	
	@RequiresPermissions("SignWish:signWish:view")
	@GetMapping()
	public String signWish()
	{
	    return prefix + "/signWish";
	}

	@RequiresPermissions("SignWish:signWish:view")
	@GetMapping("/verified")
	public String signWishVerified()
	{
		return prefix + "/signWishVerified";
	}
	
	/**
	 * 查询心愿单列表
	 */
	@RequiresPermissions("SignWish:signWish:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SignWish signWish)
	{
		signWish.setIsSy(1);
		signWish.setValid(1);
		signWish.setState(8101);
		startPage();
        List<SignWish> list = signWishService.selectSignWishList(signWish);
		return getDataTable(list);
	}


	/**
	 * 已审核心愿单列表
	 */
	@RequiresPermissions("SignWish:signWish:list")
	@PostMapping("/list2")
	@ResponseBody
	public TableDataInfo list2(SignWish signWish)
	{
		signWish.setIsSy(1);
		signWish.setValid(2);
		startPage();
		List<SignWish> list = signWishService.selectSignWishList(signWish);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出心愿单列表
	 */
	@RequiresPermissions("SignWish:signWish:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SignWish signWish)
    {
    	List<SignWish> list = signWishService.selectSignWishList(signWish);
        ExcelUtil<SignWish> util = new ExcelUtil<SignWish>(SignWish.class);
        return util.exportExcel(list, "signWish");
    }
	
	/**
	 * 新增心愿单
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存心愿单
	 */
	@RequiresPermissions("SignWish:signWish:add")
	@Log(title = "心愿单", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(SignWish signWish)
	{		
		return toAjax(signWishService.insertSignWish(signWish));
	}

	/**
	 * 修改心愿单
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		SignWish signWish = signWishService.selectSignWishById(id);
		Integer userId=signWish.getUid();
		Integer addressId=signWish.getAddressId();
		String Tps=signWish.getTps();
		String[] img=Tps.split(",");
		List<String> imgList = Arrays.asList(img);
		if(imgList.size()>0){
			String imaging=imgList.get(0);
			signWish.setTps(imaging);
		}
		User user=userService.selectUserById(userId);
		UserAddress address = new UserAddress();
		if (addressId!=null){
			address=userAddressService.selectUserAddressByIdChinese(addressId);
		}
		mmap.put("address", address);
		mmap.put("user", user);
		mmap.put("signWish", signWish);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存心愿单
	 */
	@RequiresPermissions("SignWish:signWish:edit")
	@Log(title = "心愿单", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(SignWish signWish)
	{		
		return toAjax(signWishService.updateSignWish(signWish));
	}
	
	/**
	 * 删除心愿单
	 */
	@RequiresPermissions("SignWish:signWish:remove")
	@Log(title = "心愿单", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(signWishService.deleteSignWishByIds(ids));
	}
	
	/**
	 * 同意实现心愿
	 * @throws ParseException 
	 */
	@PostMapping("/updateStatus")
	@ResponseBody
	public AjaxResult updateStatus(String id,String days,String people) throws ParseException {
		if(!StringUtils.isEmpty(id)){			
			SignWish persist=signWishService.selectSignWishById(Integer.parseInt(id));
			if(persist.getState()==8101){
				persist.setState(8102);
				String now = DateTools.getYMDhms();
				int num = Integer.parseInt(days);
				String jsDate=DateTools.plusDay(num,now);
				persist.setHelpJsDate(jsDate);
				persist.setModifyDate(DateTools.getYMDhms());
				persist.setHelpNum(Integer.parseInt(people));
				return toAjax(signWishService.updateSignWish(persist));
			} else {
				return AjaxResult.error("该心愿单并非待处理状态，请勿重复审核");
			}
		}		
		return AjaxResult.error("未找到此条心愿单！");
	}
	
	
	
	/**
	 * 金币补偿退回心愿
	 */
	@PostMapping("/updateStatus2")
	@ResponseBody
	public AjaxResult updateStatus2(String id) {
		
		Zd zd=zdService.selectZdById(8108);
		int coinCount=Integer.parseInt(zd.getName());
		SignWish persist=signWishService.selectSignWishById(Integer.parseInt(id));
		if(persist.getState()==8101){
			persist.setState(8103);
			persist.setModifyDate(DateTools.getYMDhms());
			signWishService.updateSignWish(persist);
			int userId=persist.getUid();		
			User userPersist=userService.selectUserById(userId);
			BigDecimal beforeCoin=userPersist.getCredit2();
			BigDecimal index=new BigDecimal(coinCount);
			beforeCoin=beforeCoin.add(index);
			userPersist.setCredit2(beforeCoin);
			return toAjax(userService.updateUser(userPersist));
		} else {
			return AjaxResult.error("该心愿单并非待处理状态，请勿重复审核");
		}
		
	}
	
	/**
	 * 优惠券补偿退回心愿
	 * @throws ParseException 
	 */
	@PostMapping("/updateStatus3")
	@ResponseBody
	public AjaxResult updateStatus3(String id) throws ParseException {
		if(!StringUtils.isEmpty(id)){
			SignWish persist=signWishService.selectSignWishById(Integer.parseInt(id));
			if(persist.getState()==8101){
				persist.setState(8103);
				persist.setModifyDate(DateTools.getYMDhms());
				signWishService.updateSignWish(persist);
				//字典表定义优惠券ID
				Zd zd=zdService.selectZdById(8109);
				int packetID=Integer.parseInt(zd.getName());
				
				//字典表定义优惠券有效天数
				Zd zd2=zdService.selectZdById(8110);
				int validDay=Integer.parseInt(zd2.getName());
				
				//用户ID
				int userId=persist.getUid();
				
				HbUser hbuser=new HbUser();
				hbuser.setUserId(userId);
				hbuser.setHbId(packetID);
				String head="HB_4_";
				String seq=String.valueOf((int)((Math.random()*9+1)*100000));
				String HbNum=DateTools.getDateSeqId3(head, seq);
				hbuser.setBhId(HbNum);
				hbuser.setIsYj(2);
				String nowDate= DateTools.getYMDhms();
				String endDate=DateTools.plusDay(validDay, nowDate);
				hbuser.setGqDate(endDate);
				hbuser.setValid(1);
				hbuser.setCreactDate(nowDate);
				return toAjax(hbUserService.insertHbUser(hbuser));				
			} else {
				return AjaxResult.error("该心愿单并非待处理状态，请勿重复审核");
			}
			
		}		
		return AjaxResult.error("未找到此条心愿单！");
	}
	
	
	/**
	 * 无补偿退回心愿
	 */
	@PostMapping("/updateStatus4")
	@ResponseBody
	public AjaxResult updateStatus4(String id) {
		if(!StringUtils.isEmpty(id)){
			SignWish persist=signWishService.selectSignWishById(Integer.parseInt(id));
			if(persist.getState()==8101){
				persist.setState(8103);
				persist.setModifyDate(DateTools.getYMDhms());
				return toAjax(signWishService.updateSignWish(persist));
			} else {
				return AjaxResult.error("该心愿单并非待处理状态，请勿重复审核");
			}
			
		}		
		return AjaxResult.error("未找到此条心愿单！");
	}
	
}
